package com.wlstock.funi.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.maxleap.FindCallback;
import com.maxleap.MLObject;
import com.maxleap.MLQuery;
import com.maxleap.MLQueryManager;
import com.maxleap.exception.MLException;
import com.wlstock.funi.model.StrategyFundModel;
import com.wlstock.funi.model.StrategyModel;

public class StrategyDao{
	private DataBaseHelper dbHelper;
	private static StrategyDao daoInstance;
	private Dao<StrategyModel, Integer> dao;
	
	@SuppressWarnings("unchecked")
	private StrategyDao(Context mContext){
		try {
			dbHelper = DataBaseHelper.getHelper(mContext);
			dao = dbHelper.getDao(StrategyModel.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static StrategyDao getInstance(Context mContext){
		if (daoInstance == null) {
			synchronized (StrategyDao.class) {
				if (daoInstance == null) {
					daoInstance = new StrategyDao(mContext);
				}
			}
		}
		return daoInstance;
	}
	
	public List<StrategyModel> queryAll(){
		try {
			return dao.queryForAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public int deleteAll(){
		try {
			return dao.updateRaw("delete from StrategyModel");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 返回需要存储的策略
	 * @return
     */
	public List<StrategyModel> queryNeedToSearch(){
		try {
			return dao.queryBuilder().where().eq("needShow","Y").query();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return new ArrayList<StrategyModel>();
	}



	public void createOrUpdate(StrategyModel strategyModel) {
		try {
			dao.createOrUpdate(strategyModel);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 根据短名查询全名
	 * @param shortName
	 * @return
	 */
	public String queryStraByShortName(String shortName) {
		try {
			StrategyModel model = dao.queryBuilder().where().eq("shortName", shortName).queryForFirst();
			return model.getFullName()!=null ? model.getFullName():"";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 根据key查询url
	 * @param strategyKey
	 * @return
	 */
	public String querAppendUrlByShortName(String shortName) {
		try {
			StrategyModel model = dao.queryBuilder().where().eq("shortName", shortName).queryForFirst();
			return model.getAppendURL()!=null? model.getAppendURL():"";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}


	public void sysFromServer() {
		deleteAll();
		MLQuery<MLObject> query = MLQuery.getQuery(StrategyModel.class.getSimpleName());
		MLQueryManager.findAllInBackground(query, new FindCallback<MLObject>() {
			@Override
			public void done(List<MLObject> list, MLException e) {
				if (e == null){
					for(MLObject mlObject : list){
						StrategyModel model = new StrategyModel();
						model.setValuesFromMlOjb(mlObject);
						createOrUpdate(model);
					}
				}else {
					initStrategy();
				}
			}
		});
	}

	//初始化策略
	private void initStrategy(){
		StrategyModel strategyModel = new StrategyModel("策略1","机构调研;总股本小于5亿股;股东人数小于15000;股价小于50;dde大单净入;总市值小于50亿;总市值从小到大;上市天数大于100;涨幅小于1%","",1.234f,5,0.664f,30);
		createOrUpdate(strategyModel);

		strategyModel = new StrategyModel("策略2","涨跌幅小于1%;股票简称不包含st;股票市场类型不包含创业板;换手率小于3.00%;市净率(pb)小于0.850;流通a股小于2500000000股 ;每股净资产bps大于1元;dde大单净量从大到小排名;上市天数大于100","",0.48f,30,0.67f,30);
		createOrUpdate(strategyModel);

		strategyModel = new StrategyModel("策略3","macd大于0小于0.2；macd金叉；均线多头；量比大于3；换手率从大到小；DDE大单净流入；上市天数大于100","",0.2605f,3, 0.6163f, 30);
		createOrUpdate(strategyModel);

		strategyModel = new StrategyModel("策略4","MTM买入信号；MACD>-0.15；涨幅小于5%；换手率小于3%；市值从小到大排列；","http://www.aiwencai.net/traceback/strategy/report?id=87664&qs=backtest_open_tab1###",0.9637f,2, 0.6348f, 5);
		createOrUpdate(strategyModel);

		strategyModel = new StrategyModel("策略5","涨跌幅小于1% ;股票简称不包含st ;换手率小于2.00% ;市净率(pb)小于0.850 ;流通a股小于2500000000股 ;每股净资产bps大于1元 ; dde大单净量从大到小排名 ; 次日一字线取反","http://www.aiwencai.net/traceback/strategy/report?id=85614&qs=backtest_open_tab1###",0.3905f,30, 0.6667f, 30);
		createOrUpdate(strategyModel);

		strategyModel = new StrategyModel("策略6","涨跌幅小于1% ;股票简称不包含st ;股票市场类型不包含创业板 ;换手率小于3.00% ;市净率(pb)小于0.850 ;流通a股小于2500000000股 ;每股净资产bps大于1元 ; dde大单净量小到大排名","http://www.aiwencai.net/traceback/strategy/report?id=88076&qs=backtest_open_tab1",0.5387f,20, 0.7083f, 30);
		createOrUpdate(strategyModel);

		strategyModel = new StrategyModel("策略7","macd(macd大于0.00且小于0.20) ; macd金叉 ; 5日线10日线20日线30日线60日线120日线多头排列 ;量比大于4小于7 ;总市值小于600亿 ; 换手率从大到小排名","http://www.aiwencai.net/traceback/strategy/report?id=74765&qs=backtest_open_tab1",0.3689f,10, 0.5705f, 3);
		createOrUpdate(strategyModel);

		strategyModel = new StrategyModel("策略8","BBI大于5.1；BBI小于9.3；量比大于0.5小于1.1；换手率大于1%小于5%；市值从小到大","http://www.aiwencai.net/traceback/strategy/report?id=87646&qs=backtest_open_tab1",1.9561f,5, 0.7095f, 30);
		createOrUpdate(strategyModel);
	}
}
